package com.async_export_demo.config;

import com.async_export_demo.export.DataProvider;
import com.async_export_demo.export.ExportEnum;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;
import java.util.stream.Collectors;

@Configuration
@MapperScan("com.async_export_demo.mapper")
public class SystemConfig {

    @Autowired
    private ApplicationContext applicationContext;

    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

    @Bean(name = "dataProviderMap") // 如果你想要通过名字访问这个bean，可以这样定义它，否则可以省略这个注解和name属性。
    public Map<ExportEnum, DataProvider> dataProviderMap() {
        return applicationContext.getBeansOfType(DataProvider.class) // 获取所有DataProvider类型的bean
                .values().stream() // 转换为流操作以便于处理和收集
                .collect(Collectors.toMap(DataProvider::getType, bean -> bean)); // 收集到Map中，以类名为key，bean为value。你也可以自定义key。
    }
}